package com.zjw.dao.impl;

import com.zjw.dao.BaseDao;
import com.zjw.entity.TradeLog;
import com.zjw.dao.TradeLogDao;
import com.zjw.utils.ConnectionUtil;
import com.zjw.vo.TradeLogDetail;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 交易记录表(Tradelog)表数据库访问层实现类
 *
 * @author 翟
 * @since 2025-04-09 09:21:58
 */
public class TradelogDaoImpl extends BaseDao implements TradeLogDao {

    public TradelogDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public int insert(TradeLog tradeLog) throws SQLException {
        String sql = "INSERT INTO tradelog (tradelog_id , buyer_id , goods_id , seller_id , transaction_time , receive_info) VALUES (? , ?, ?, ?, ?, ?)";
        return executeUpdate(sql, tradeLog.getTradeLogId(), tradeLog.getBuyerId(), tradeLog.getGoodsId(),
                tradeLog.getSellerId(), tradeLog.getTransactionTime(), tradeLog.getReceiveInfo());
    }

    @Override
    public List<TradeLogDetail> selectAll() throws SQLException {
        String sql = "select log.tradelog_id ,  u.user_name as buyer, g.goods_name , u1.user_name as seller , log.receive_info from tradelog log\n" +
                "inner join user u on log.buyer_id = u.user_id\n" +
                "inner join user u1 on log.seller_id = u1.user_id\n" +
                "inner join goods g on log.goods_id = g.goods_id";
        ResultSet resultSet = null;
        List<TradeLogDetail> list = new ArrayList<>();
        try {
            resultSet = executeQuery(sql);
            while (resultSet.next()) {
                TradeLogDetail tradeLogDetail = new TradeLogDetail();
                tradeLogDetail.setLodId(resultSet.getInt("tradelog_id"));
                tradeLogDetail.setBuyerName(resultSet.getString("buyer"));
                tradeLogDetail.setGoodsName(resultSet.getString("goods_name"));
                tradeLogDetail.setSellerName(resultSet.getString("seller"));
                tradeLogDetail.setAddress(resultSet.getString("receive_info"));
                list.add(tradeLogDetail);
            }
            return list;
        } finally {
            ConnectionUtil.closeALL(resultSet, null, null);
        }
    }
}
